package cn.bdqn.veh.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.mapper.LeaderMapper;
import cn.bdqn.veh.model.Leader;
import cn.bdqn.veh.model.LeaderExample;
import cn.bdqn.veh.service.LeaderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-19 11:42
 */
@Service
public class LeaderServiceImpl extends BaseService implements LeaderService {

    @Resource
    LeaderMapper leaderMapper;

    @Override
    public EUList<Leader> list(PageBean pageBean, String lname, String phone, String idcard) {
        LeaderExample example = new LeaderExample();
        LeaderExample.Criteria criteria = example.createCriteria();
        criteria.andIsdelEqualTo(false);
        criteria.andEidEqualTo(getEid());   //从session来
        if(!StringUtils.isEmpty(lname) || !StringUtils.isEmpty(phone)  || !StringUtils.isEmpty(idcard)){
//            criteria.andAccountEqualTo(username);
            criteria.andLnameLike("%"+lname+"%");
            criteria.andPhoneLike("%"+phone+"%");
            criteria.andIdcardLike("%"+idcard+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Leader> leaders = leaderMapper.selectByExample(example);
        PageInfo<Leader> info=new PageInfo<>(leaders);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Leader leader) {
        if(leader.getId()==null){
            leader.setEid(getEid());

            leaderMapper.insertSelective(leader);
        }else{
            leaderMapper.updateByPrimaryKeySelective(leader);    //更新时不更新帐号
        }
        return R.ok();
    }

    @Override
    public R delete(Integer id) {
        Leader leader = new Leader();
        leader.setId(id);
        leader.setIsdel(true);
//        try{
        leaderMapper.updateByPrimaryKeySelective(leader);
        return R.ok();
    }
}
